/*
 * @(#)ApiController.java 1.0 2019年11月4日下午2:46:20
 *
 * 德邦证券股份有限公司 - 科技金融部
 * Copyright (c) 2012-2019 HexunFSD, Inc. All rights reserved.
 */
package com.saturn.config.server.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSONObject;
import com.saturn.config.server.base.dto.ConfigInfo;
import com.saturn.config.server.service.AppConfigService;
import com.saturn.config.server.util.SignUtil;

import cn.hutool.extra.servlet.ServletUtil;

/**
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	none
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author tanks
 * @version 1.0, 2019年11月4日
 * @since saturn-jdbc-config-center-admin
 * 
 */
@Controller
public class ApiController {

	@Autowired 
	private AppConfigService appConfigService;
	@Value("${app.sign.privite.key:6e776f703868396a7370773131307077}")
	private String priKey;
	
	/**
	 * API查询接口
	 * @param environment
	 * @param projectName
	 * @param ip
	 * @param verifyCode
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/api/queryAppInfo.htm")
	public void appInfo(HttpServletRequest request,HttpServletResponse response,String envType,String appName,String version,String sign) throws IOException{
		Map<String,Object> parmas = WebUtils.getParametersStartingWith(request, StringUtils.EMPTY);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		//检查参数是否符合要求
		if( StringUtils.isBlank(envType) 
				|| StringUtils.isBlank(appName) 
				|| StringUtils.isBlank(version) 
				|| StringUtils.isBlank(sign)) {
			Map<String,String> result = new HashMap<String,String>();
			result.put("result", "fail");
			result.put("message", "必要参数为空，获取失败");
			writer.write(JSONObject.toJSONString(result));
			writer.close();
			return;
		}
		boolean verify = SignUtil.verifySign(this.priKey, parmas);
		if(!verify){
			Map<String,String> result = new HashMap<String,String>();
			result.put("result", "fail");
			result.put("message", "数据签名验证失败");
			writer.write(JSONObject.toJSONString(result));
			writer.close();
			return;
		}
		//如果不存在pubVersion，则取默认的version版本值
		if( !parmas.containsKey("pubVersion") ) {
			parmas.put("pubVersion", parmas.get("version"));
		}
		List<ConfigInfo> appInfo = this.appConfigService.queryAppConfigs(parmas);
		
		String resutJson = JSONObject.toJSONString(appInfo);
		writer.write(resutJson);
		writer.close();
	}
	
	/**
	 * 注册应用实例,用于服务器监听实例，用于推送数据
	 * @param request
	 * @param response
	 * @param envType
	 * @param appName
	 * @param version
	 * @param sign
	 * @throws IOException
	 */
	@RequestMapping(value = "/api/registAppliation.htm")
	public void registAppliation(HttpServletRequest request,HttpServletResponse response,String envType,String appName,String version,String sign) throws IOException{
		
	}
	
	/**
	 * 取消注册应用登记
	 * @param request
	 * @param response
	 * @param envType
	 * @param appName
	 * @param version
	 * @param sign
	 * @throws IOException
	 */
	@RequestMapping(value = "/api/unregistAppliation.htm")
	public void unregistAppliation(HttpServletRequest request,HttpServletResponse response,String envType,String appName,String version,String sign) throws IOException{
		
	}
	
}
